<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>主席树、函数式线段树、可持久化线段树 初步 学习笔记 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="主席树入门笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="主席树、函数式线段树、可持久化线段树 初步 学习笔记">
<meta property="og:url" content="http://example.com/2022/02/11/%E4%B8%BB%E5%B8%AD%E6%A0%91%E3%80%81%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BA%BF%E6%AE%B5%E6%A0%91%E3%80%81%E5%8F%AF%E6%8C%81%E4%B9%85%E5%8C%96%E7%BA%BF%E6%AE%B5%E6%A0%91%20%E5%88%9D%E6%AD%A5%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="主席树入门笔记">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cdn.luogu.com.cn/upload/image_hosting/pg9hkziz.png">
<meta property="og:image" content="https://cdn.luogu.com.cn/upload/image_hosting/klin66vg.png">
<meta property="article:published_time" content="2022-02-11T11:46:57.450Z">
<meta property="article:modified_time" content="2023-07-19T11:51:05.279Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.luogu.com.cn/upload/image_hosting/pg9hkziz.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-主席树、函数式线段树、可持久化线段树 初步 学习笔记" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2022/02/11/%E4%B8%BB%E5%B8%AD%E6%A0%91%E3%80%81%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BA%BF%E6%AE%B5%E6%A0%91%E3%80%81%E5%8F%AF%E6%8C%81%E4%B9%85%E5%8C%96%E7%BA%BF%E6%AE%B5%E6%A0%91%20%E5%88%9D%E6%AD%A5%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="article-date">
  <time class="dt-published" datetime="2022-02-11T11:46:57.450Z" itemprop="datePublished">2022-02-11</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      主席树、函数式线段树、可持久化线段树 初步 学习笔记
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>主席树入门笔记</p>
<h1 id="主席树-函数式线段树-可持久化线段树-初步-学习笔记"><a href="#主席树-函数式线段树-可持久化线段树-初步-学习笔记" class="headerlink" title="主席树/函数式线段树/可持久化线段树 初步 学习笔记"></a>主席树/函数式线段树/可持久化线段树 初步 学习笔记</h1><h2 id="1-什么是主席树？"><a href="#1-什么是主席树？" class="headerlink" title="1.什么是主席树？"></a>1.什么是主席树？</h2><p>主席树是一种由许多棵 <strong>重叠的</strong> 值域线段树构成的数据结构，可以维护很多跟值域有关的信息。</p>
<h2 id="2-怎么写主席树？"><a href="#2-怎么写主席树？" class="headerlink" title="2.怎么写主席树？"></a>2.怎么写主席树？</h2><p>先来看一道例题（区间第 $k$ 小）：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P3834">洛谷P3834</a> 【模板】可持久化线段树 2</p>
<p>题目大意:给定 $n$ 个整数构成的序列 $a$，将对于指定的闭区间 $[l,r]$ 查询其区间内的第 $k$ 小值。</p>
</blockquote>
<p>先离散化一下。考虑用值域线段树来维护这个东西，一个非常暴力的思路就是建 $n$ 棵线段树，第 $i$ 棵线段树维护区间 $[1,i]$ 的信息，对于区间 $[l,r]$ 只需把第 $r$ 棵线段树和第 $l-1$ 棵线段树做差再查第 $k$ 小。这样做的时间复杂度是 $O(n^2)$ 的，时间也是 $O(n^2)$ 的 <del>（时空两开花）</del> ，显然会炸。</p>
<p>有没有优化的方法？当然有！</p>
<p>注意到一件事：</p>
<p><img src="https://cdn.luogu.com.cn/upload/image_hosting/pg9hkziz.png" alt=""></p>
<p>如果把其他的节点复制一遍显然太浪费了，所以可以在新建一棵线段树时复用这些节点。像这样：</p>
<p><img src="https://cdn.luogu.com.cn/upload/image_hosting/klin66vg.png" alt=""></p>
<p>如果我们把绿色树根作为根节点，对这颗新线段树进行访问，它的效果跟完全新建一棵线段树是一样的，却只使用 $O(\log{n})$ 的时间和空间，非常的高效。这就是 <strong>可持久化</strong> 的思想。</p>
<p>代码：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> MAXN=<span class="number">200000</span>;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">segment_tree</span>&#123;</span><br><span class="line">    <span class="type">int</span> sum;</span><br><span class="line">    <span class="type">int</span> lp,rp;</span><br><span class="line">    segment_tree *ls,*rs;</span><br><span class="line">    <span class="built_in">segment_tree</span>()&#123;sum=<span class="number">0</span>;lp=rp=<span class="number">0</span>;ls=rs=<span class="literal">NULL</span>;&#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">build</span><span class="params">(<span class="type">int</span> l,<span class="type">int</span> r)</span></span>&#123;<span class="comment">//新建一棵空白树</span></span><br><span class="line">        lp=l;rp=r;</span><br><span class="line">        <span class="keyword">if</span>(l&lt;r)&#123;</span><br><span class="line">            <span class="type">int</span> mid=(l+r)/<span class="number">2</span>;</span><br><span class="line">            ls=<span class="keyword">new</span> segment_tree;</span><br><span class="line">            ls-&gt;<span class="built_in">build</span>(l,mid);</span><br><span class="line">            rs=<span class="keyword">new</span> segment_tree;</span><br><span class="line">            rs-&gt;<span class="built_in">build</span>(mid+<span class="number">1</span>,r);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">push_up</span><span class="params">()</span></span>&#123;</span><br><span class="line">        sum=ls-&gt;sum+rs-&gt;sum;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function">segment_tree* <span class="title">add</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> k)</span></span>&#123;<span class="comment">//可持久化地修改</span></span><br><span class="line">        segment_tree* res=<span class="keyword">new</span> <span class="built_in">segment_tree</span>(*<span class="keyword">this</span>);</span><br><span class="line">        <span class="keyword">if</span>(lp==rp)&#123;</span><br><span class="line">            res-&gt;sum+=k;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(x&lt;=ls-&gt;rp)&#123;</span><br><span class="line">                res-&gt;ls=ls-&gt;<span class="built_in">add</span>(x,k);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(x&gt;=rs-&gt;lp)&#123;</span><br><span class="line">                res-&gt;rs=rs-&gt;<span class="built_in">add</span>(x,k);</span><br><span class="line">            &#125;</span><br><span class="line">            res-&gt;<span class="built_in">push_up</span>();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">kth</span><span class="params">(segment_tree*l,segment_tree*r,<span class="type">int</span> k)</span></span>&#123;<span class="comment">//求第k大</span></span><br><span class="line">    <span class="keyword">if</span>(l-&gt;lp==l-&gt;rp)&#123;</span><br><span class="line">        <span class="keyword">return</span> l-&gt;lp;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="type">int</span> lcnt=r-&gt;ls-&gt;sum-l-&gt;ls-&gt;sum;<span class="comment">//做差</span></span><br><span class="line">        <span class="keyword">if</span>(k&lt;=lcnt)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">kth</span>(l-&gt;ls,r-&gt;ls,k);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">kth</span>(l-&gt;rs,r-&gt;rs,k-lcnt);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> n,m,a[MAXN+<span class="number">5</span>];</span><br><span class="line">segment_tree *trees[MAXN+<span class="number">5</span>];</span><br><span class="line">map&lt;<span class="type">int</span>,<span class="type">int</span>&gt; mp1;</span><br><span class="line"><span class="type">int</span> mp2[MAXN+<span class="number">5</span>];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">lsh</span><span class="params">()</span></span>&#123;<span class="comment">//离散化</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        mp2[i]=a[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">sort</span>(mp2+<span class="number">1</span>,mp2+<span class="number">1</span>+n);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        mp1[mp2[i]]=i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        a[i]=mp1[a[i]];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    ios::<span class="built_in">sync_with_stdio</span>(<span class="literal">false</span>);</span><br><span class="line">    cin&gt;&gt;n&gt;&gt;m;</span><br><span class="line">    trees[<span class="number">0</span>]=<span class="keyword">new</span> segment_tree;</span><br><span class="line">    trees[<span class="number">0</span>]-&gt;<span class="built_in">build</span>(<span class="number">1</span>,n);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        cin&gt;&gt;a[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">lsh</span>();</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;<span class="comment">//建主席树</span></span><br><span class="line">        trees[i]=trees[i<span class="number">-1</span>]-&gt;<span class="built_in">add</span>(a[i],<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">        <span class="type">int</span> l,r,k;cin&gt;&gt;l&gt;&gt;r&gt;&gt;k;</span><br><span class="line">        cout&lt;&lt;mp2[<span class="built_in">kth</span>(trees[l<span class="number">-1</span>],trees[r],k)]&lt;&lt;endl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="3-主席树的效率如何？"><a href="#3-主席树的效率如何？" class="headerlink" title="3.主席树的效率如何？"></a>3.主席树的效率如何？</h2><p>主席树通过这道例题的时空复杂度均为 $O(n\log{n})$，非常高效。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/02/11/%E4%B8%BB%E5%B8%AD%E6%A0%91%E3%80%81%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BA%BF%E6%AE%B5%E6%A0%91%E3%80%81%E5%8F%AF%E6%8C%81%E4%B9%85%E5%8C%96%E7%BA%BF%E6%AE%B5%E6%A0%91%20%E5%88%9D%E6%AD%A5%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-id="clk9o8ln400088wi51hym5cnb" data-title="主席树、函数式线段树、可持久化线段树 初步 学习笔记" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2022/02/14/%E9%A2%98%E8%A7%A3%20%E6%B4%9B%E8%B0%B7%20P1763%20%E5%9F%83%E5%8F%8A%E5%88%86%E6%95%B0/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          题解 洛谷 P1763 埃及分数
        
      </div>
    </a>
  
  
    <a href="/2022/01/26/%E5%B9%B3%E8%A1%A1%E6%A0%91%20Treap%EF%BC%88%E6%A0%91%E5%A0%86%EF%BC%89%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">平衡树 Treap（树堆） 学习笔记</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>